package org.vincen.helloworld.jacob;

import java.util.List;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ExportWord {

	public int export2Word(String path, String fileName, List<Object[]> list) {
		try {
			if (null == list || 0 >= list.size()) {
				return 0;
			}
			// 代表一个word 程序
			// 加载一个Word.Application这个com组件
			ActiveXComponent MsWordApp = new ActiveXComponent(
					"Word.Application");

			// 代表进行处理的word 文档
			Dispatch document = null;

			MsWordApp.setProperty("Visible", new Variant(false));

			Dispatch documents = Dispatch.get(MsWordApp, "Documents")
					.toDispatch();
			document = Dispatch.call(documents, "Add").toDispatch();

			Dispatch selection = Dispatch.get(MsWordApp, "Selection")
					.toDispatch(); // 输入内容需要的对象

			Dispatch.call(selection, "TypeText", "案件详细列表"); // 写入标题内容 // 标题格行

			Dispatch wordContent = Dispatch.get(document, "Content")
					.toDispatch(); // 取得word文件的内容
			Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs")
					.toDispatch(); // 所有段落
			int paragraphCount = Dispatch.get(paragraphs, "Count")
					.changeType(Variant.VariantInt).getInt();// 一共的段落数
			// 找到刚输入的段落，设置格式
			Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
					new Variant(paragraphCount)).toDispatch(); // 最后一段（也就是刚插入的）
			// Range 对象表示文档中的一个连续范围，由一个起始字符位置和一个终止字符位置定义
			Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range")
					.toDispatch();
			Dispatch font = Dispatch.get(lastParagraphRange, "Font")
					.toDispatch();
			Dispatch.put(font, "Bold", new Variant(false)); // 设置为黑体
			Dispatch.put(font, "Italic", new Variant(false)); // 设置为斜体
			Dispatch.put(font, "Name", new Variant("宋体")); //
			Dispatch.put(font, "Size", new Variant(12)); // 小四

			// 建立表格
			Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
			Dispatch range = Dispatch.get(selection, "Range").toDispatch();// /当前光标位置或者选中的区域
			int row = list.size();
			int column = list.get(0).length;
			Dispatch newTable = Dispatch.call(tables, "Add", range,
					new Variant(row), new Variant(column), new Variant(1))
					.toDispatch(); // 设置row,column,表格外框宽度
			Dispatch cols = Dispatch.get(newTable, "Columns").toDispatch(); // 此表的所有列，
			int colCount = Dispatch.get(cols, "Count")
					.changeType(Variant.VariantInt).getInt();// 一共有多少列
			// 实际上这个数==column
			for (int i = 1; i <= colCount; i++) { // 循环取出每一列

				Dispatch col = Dispatch.call(cols, "Item", new Variant(i))
						.toDispatch();
				Dispatch cells = Dispatch.get(col, "Cells").toDispatch();// 当前列中单元格
				int cellCount = Dispatch.get(cells, "Count")
						.changeType(Variant.VariantInt).getInt();// 当前列中单元格数
				// 实际上这个数等于row
				for (int j = 1; j <= cellCount; j++) {// 每一列中的单元格数
					Object[] objs = list.get(j - 1);
					Dispatch cell = Dispatch.call(newTable, "Cell",
							new Variant(j), new Variant(i)).toDispatch();
					Dispatch.call(cell, "Select");
					Dispatch sel = Dispatch.get(MsWordApp, "Selection")
							.toDispatch(); // 输入内容需要的对象
					if (null == objs[i - 1]) {
						Dispatch.put(sel, "Text", "");
					} else {
						Dispatch.put(sel, "Text", objs[i - 1].toString());
					}
				}
			}

			Dispatch.call(document, "SaveAs", path + fileName);

			Dispatch.call(document, "Close", new Variant(0));
			document = null;

			Dispatch.call(MsWordApp, "Quit");
			MsWordApp = null;
			document = null;
			return 1;
		} catch (Exception ex) {
			return -1;
		}
	}

	public int export2Word(String path, String fileName, String tableName,
			List<Object[]> list) {
		try {
			if (null == list || 0 >= list.size()) {
				return 0;
			}
			// 代表一个word 程序
			ActiveXComponent MsWordApp = new ActiveXComponent(
					"Word.Application");

			// 代表进行处理的word 文档
			Dispatch document = null;

			MsWordApp.setProperty("Visible", new Variant(false));

			Dispatch documents = Dispatch.get(MsWordApp, "Documents")
					.toDispatch();
			document = Dispatch.call(documents, "Add").toDispatch();

			Dispatch selection = Dispatch.get(MsWordApp, "Selection")
					.toDispatch(); // 输入内容需要的对象

			Dispatch.call(selection, "TypeText", tableName); // 写入标题内容 // 标题格行

			Dispatch wordContent = Dispatch.get(document, "Content")
					.toDispatch(); // 取得word文件的内容
			Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs")
					.toDispatch(); // 所有段落
			int paragraphCount = Dispatch.get(paragraphs, "Count")
					.changeType(Variant.VariantInt).getInt();// 一共的段落数
			// 找到刚输入的段落，设置格式
			Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
					new Variant(paragraphCount)).toDispatch(); // 最后一段（也就是刚插入的）
			// Range 对象表示文档中的一个连续范围，由一个起始字符位置和一个终止字符位置定义
			Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range")
					.toDispatch();
			Dispatch font = Dispatch.get(lastParagraphRange, "Font")
					.toDispatch();
			Dispatch.put(font, "Bold", new Variant(false)); // 设置为黑体
			Dispatch.put(font, "Italic", new Variant(false)); // 设置为斜体
			Dispatch.put(font, "Name", new Variant("宋体")); //
			Dispatch.put(font, "Size", new Variant(12)); // 小四

			// Dispatch selection2 = Dispatch.get(MsWordApp,
			// "Selection").toDispatch(); // 输入内容需要的对象
			// Dispatch alignment = Dispatch.get(selection2,
			// "ParagraphFormat").toDispatch();// 段落格式
			// Dispatch.put(alignment, "Alignment", "1"); // (1:置中 2:靠右 3:靠左)
			// Dispatch.call(selection2, "TypeParagraph");// 插入一个空行
			//
			// wordContent = Dispatch.get(document, "Content").toDispatch(); //
			// 取得word文件的内容
			// paragraphs = Dispatch.get(wordContent,
			// "Paragraphs").toDispatch(); // 所有段落
			// paragraphCount = Dispatch.get(paragraphs,
			// "Count").changeType(Variant.VariantInt).getInt();// 一共的段落数
			// // 找到刚输入的段落，设置格式
			// lastParagraph = Dispatch.call(paragraphs, "Item", new
			// Variant(paragraphCount)).toDispatch(); // 最后一段（也就是刚插入的）
			// // Range 对象表示文档中的一个连续范围，由一个起始字符位置和一个终止字符位置定义
			// lastParagraphRange = Dispatch.get(lastParagraph,
			// "Range").toDispatch();
			// font = Dispatch.get(lastParagraphRange, "Font").toDispatch();
			// Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体
			// Dispatch.put(font, "Italic", new Variant(false)); // 设置为斜体
			// Dispatch.put(font, "Name", new Variant("宋体")); //
			// Dispatch.put(font, "Size", new Variant(12)); // 小四
			//
			// selection2 = Dispatch.get(MsWordApp, "Selection").toDispatch();
			// // 输入内容需要的对象
			// alignment = Dispatch.get(selection2,
			// "ParagraphFormat").toDispatch();// 段落格式
			// Dispatch.put(alignment, "Alignment", "3"); // (1:置中 2:靠右 3:靠左)

			// 建立表格
			Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
			Dispatch range = Dispatch.get(selection, "Range").toDispatch();// /当前光标位置或者选中的区域
			int row = list.size();
			int column = list.get(0).length;
			Dispatch newTable = Dispatch.call(tables, "Add", range,
					new Variant(row), new Variant(column), new Variant(1))
					.toDispatch(); // 设置row,column,表格外框宽度
			Dispatch cols = Dispatch.get(newTable, "Columns").toDispatch(); // 此表的所有列，
			int colCount = Dispatch.get(cols, "Count")
					.changeType(Variant.VariantInt).getInt();// 一共有多少列
			// 实际上这个数==column
			for (int i = 1; i <= colCount; i++) { // 循环取出每一列

				Dispatch col = Dispatch.call(cols, "Item", new Variant(i))
						.toDispatch();
				Dispatch cells = Dispatch.get(col, "Cells").toDispatch();// 当前列中单元格
				int cellCount = Dispatch.get(cells, "Count")
						.changeType(Variant.VariantInt).getInt();// 当前列中单元格数
				// 实际上这个数等于row
				for (int j = 1; j <= cellCount; j++) {// 每一列中的单元格数
					Object[] objs = list.get(j - 1);
					Dispatch cell = Dispatch.call(newTable, "Cell",
							new Variant(j), new Variant(i)).toDispatch();
					Dispatch.call(cell, "Select");
					Dispatch sel = Dispatch.get(MsWordApp, "Selection")
							.toDispatch(); // 输入内容需要的对象
					if (null == objs[i - 1]) {
						Dispatch.put(sel, "Text", "");
					} else {
						Dispatch.put(sel, "Text", objs[i - 1].toString());
					}
					// if (j == 1) {
					// wordContent = Dispatch.get(document,
					// "Content").toDispatch(); // 取得word文件的内容
					// paragraphs = Dispatch.get(wordContent,
					// "Paragraphs").toDispatch(); // 所有段落
					// paragraphCount = Dispatch.get(paragraphs,
					// "Count").changeType(Variant.VariantInt).getInt();//
					// 一共的段落数
					// // 找到刚输入的段落，设置格式
					// lastParagraph = Dispatch.call(paragraphs, "Item", new
					// Variant(paragraphCount)).toDispatch(); // 最后一段（也就是刚插入的）
					// // Range 对象表示文档中的一个连续范围，由一个起始字符位置和一个终止字符位置定义
					// lastParagraphRange = Dispatch.get(lastParagraph,
					// "Range").toDispatch();
					// font = Dispatch.get(lastParagraphRange,
					// "Font").toDispatch();
					// Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体
					// Dispatch.put(font, "Italic", new Variant(false)); //
					// 设置为斜体
					// Dispatch.put(font, "Name", new Variant("宋体")); //
					// Dispatch.put(font, "Size", new Variant(12)); // 小四
					//
					// selection2 = Dispatch.get(MsWordApp,
					// "Selection").toDispatch(); // 输入内容需要的对象
					// alignment = Dispatch.get(selection2,
					// "ParagraphFormat").toDispatch();// 段落格式
					// Dispatch.put(alignment, "Alignment", "1"); // (1:置中 2:靠右
					// // 3:靠左)
					// } else {
					// wordContent = Dispatch.get(document,
					// "Content").toDispatch(); // 取得word文件的内容
					// paragraphs = Dispatch.get(wordContent,
					// "Paragraphs").toDispatch(); // 所有段落
					// paragraphCount = Dispatch.get(paragraphs,
					// "Count").changeType(Variant.VariantInt).getInt();//
					// 一共的段落数
					// // 找到刚输入的段落，设置格式
					// lastParagraph = Dispatch.call(paragraphs, "Item", new
					// Variant(paragraphCount)).toDispatch(); // 最后一段（也就是刚插入的）
					// // Range 对象表示文档中的一个连续范围，由一个起始字符位置和一个终止字符位置定义
					// lastParagraphRange = Dispatch.get(lastParagraph,
					// "Range").toDispatch();
					// font = Dispatch.get(lastParagraphRange,
					// "Font").toDispatch();
					// Dispatch.put(font, "Bold", new Variant(false)); // 设置为黑体
					// Dispatch.put(font, "Italic", new Variant(false)); //
					// 设置为斜体
					// Dispatch.put(font, "Name", new Variant("宋体")); //
					// Dispatch.put(font, "Size", new Variant(12)); // 小四
					//
					// selection2 = Dispatch.get(MsWordApp,
					// "Selection").toDispatch(); // 输入内容需要的对象
					// alignment = Dispatch.get(selection2,
					// "ParagraphFormat").toDispatch();// 段落格式
					// Dispatch.put(alignment, "Alignment", "3"); // (1:置中 2:靠右
					// 3:靠左)
					// }
				}
			}

			Dispatch.call(document, "SaveAs", path + fileName);

			Dispatch.call(document, "Close", new Variant(0));
			document = null;

			Dispatch.call(MsWordApp, "Quit");
			MsWordApp = null;
			document = null;
			return 1;
		} catch (Exception ex) {
			return -1;
		}
	}
}
